//
//  AgeViewController.m
//  NuwaRobot
//
//  Created by Kendama on 2017/12/1.
//  Copyright © 2017年 Kendama. All rights reserved.
//

#import "AgeViewController.h"
#import "CustomPickerView.h"
#import "PublicSetUserInfoApi.h"
#import "DateTools.h"

@interface AgeViewController ()<CustomPickerViewDelegate>


@property(nonatomic,strong) CustomPickerView *customView;
@property(nonatomic,strong) NSMutableArray *dataSource;
@property(nonatomic,strong) UIButton *confirmButton;


@end

@implementation AgeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}


#pragma mark --- 点击事件
- (void)didClickConfirmButton
{
    NSString *dateString = [self getDateStringWithAge:self.age];
    PublicSetUserInfoApi *api = [[PublicSetUserInfoApi alloc] initWithDic:@{@"birthday":dateString}];
    [api dtStartYTKRequestCompletionBlockWithSuccess:^(YTKBaseRequest * _Nonnull request) {
        [SVProgressHUD showSuccessWithStatus:@"修改成功"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.7 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.navigationController popViewControllerAnimated:YES];
        });
    } failure:nil];
}

- (NSString *)getDateStringWithAge:(NSString *)age
{
    NSDate *date = [NSDate date];
    NSString *nowString = [date formattedDateWithFormat:@"YYYY-MM-dd"];
    NSArray *valueList = [nowString componentsSeparatedByString:@"-"];
    NSMutableArray *array = [NSMutableArray array];
    for (NSString *str in valueList) {
        [array addObject:@(str.intValue).stringValue];
    }
    array[0] = [NSString stringWithFormat:@"%i",[valueList.firstObject intValue] - [age intValue]];
    NSString *dateString = [NSString string];
    if (valueList.count == 3) {
        dateString = [NSString stringWithFormat:@"%@-01-01",array[0]];
    }
    return dateString;
}

#pragma mark --- 懒加载
- (CustomPickerView *)customView
{
    if(!_customView)
    {
        UIView *superView = self.view;
        _customView = [[CustomPickerView alloc] init];
        _customView.customPickerViewDelegate = self;
        _customView.datasource = [NSMutableArray arrayWithArray:self.dataSource];
        _customView.placeLabel.text = @"岁";
        _customView.shadeView.backgroundColor = rgba(88,111,161,1);
        [_customView showLocationByValue:self.age];
        [superView addSubview:_customView];
    }
    return _customView;
}

- (NSMutableArray *)dataSource
{
    if(!_dataSource)
    {
        _dataSource = [[NSMutableArray alloc]init];
        for (int i = 1; i <= 100; i++) {
            NSString *string = [NSString stringWithFormat:@"%i",i];
            [_dataSource addObject:string];
        }
    }
    return _dataSource;
}


- (UIButton *)confirmButton
{
    if(!_confirmButton)
    {
        UIView *superView = self.view;
        _confirmButton = [[UIButton alloc]init];
        [superView addSubview:_confirmButton];
        [_confirmButton setTitle:@"提交" forState:UIControlStateNormal];
        [_confirmButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_confirmButton.titleLabel setFont:fontCommonSize];
        [_confirmButton setBackgroundColor:rgba(0,159,236,1)];
        _confirmButton.layer.cornerRadius = 5;
        [_confirmButton addTarget:self action:@selector(didClickConfirmButton) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _confirmButton;
}

#pragma mark - 代理
- (void)customPickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    self.age = self.dataSource[row];
}

- (void)masLayoutSubview
{
    __weak UIView *superView = self.view;

    [self.customView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(superView).with.offset(px1920Height(300));
        make.left.right.mas_equalTo(superView);
        make.height.mas_equalTo(px1920Height(900));
    }];
    
    [self.confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.customView.mas_bottom).with.offset(px1920Height(220));
        make.left.mas_equalTo(superView.mas_left).with.offset(px1080Width(40));
        make.right.mas_equalTo(superView.mas_right).with.offset(-px1080Width(40));
        make.height.mas_equalTo(px1920Height(136));
    }];
    
    
}


- (void)setNavigationStyle
{
    [self setNavigationBarTitle:@"年龄"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
